﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace Common
{
    public class AppFileHelper
    {
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="keyName">键值名称</param>
        /// <param name="keyValue">内容</param>
        public static void addItem(string keyName, string keyValue)
        {
            //添加配置文件的项，键为keyName，值为keyValue  
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Add(keyName, keyValue);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        /// <summary>
        /// 判断键为keyName的项是否存在
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public bool existItem(string keyName)
        {
            //判断配置文件中是否存在键为keyName的项  
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == keyName)
                {
                    //存在  
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 获取键为keyName的项的值
        /// </summary>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static string valueItem(string keyName)
        {
            //返回配置文件中键为keyName的项的值  
            return ConfigurationManager.AppSettings[keyName];
        }
        /// <summary>
        /// 修改键为keyName的项的值
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="newKeyValue"></param>
        public static void modifyItem(string keyName, string newKeyValue)
        {
            try
              {
                  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                  if (config.AppSettings.Settings[keyName] != null)
                      config.AppSettings.Settings[keyName].Value = newKeyValue;
                else
                      config.AppSettings.Settings.Add(keyName, newKeyValue);
                 config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                 //return true;
             }
              catch
              {
                 // return false;
              }
        }
        /// <summary>
        /// 删除键为keyName的项
        /// </summary>
        /// <param name="keyName"></param>
        public static void removeItem(string keyName)
        {
            //删除配置文件键为keyName的项  
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove(keyName);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }  
    }
}
